<template>
  <div>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="userAdd-form"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          type="text"
          v-model="ruleForm.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex" size="medium">
          <el-radio border label="男"></el-radio>
          <el-radio border label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      </el-form-item> 
      <el-form-item label="在职部门" prop="department">
        <el-select v-model="ruleForm.department">
          <div v-for="ele in depart">
            <el-option :label="ele" :value="ele"></el-option>
          </div>
        </el-select>
      </el-form-item>
      <el-form-item label="在职职位" prop="position">
        <el-radio-group v-model="ruleForm.position" size="medium">
          <el-radio border label="普通员工"></el-radio>
          <el-radio border label="项目组长"></el-radio>
          <el-radio border label="产品经理"></el-radio>
          <el-radio border label="部门经理"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="入职日期" prop="date">
        <el-date-picker
          v-model="ruleForm.date"
          autocomplete="off"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="联系电话" prop="tel">
        <el-input
          type="tel"
          v-model="ruleForm.tel"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input
          type="email"
          v-model="ruleForm.email"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          type="text"
          v-model="ruleForm.address"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="在职状态" prop="status">
        <el-switch
          @change="switchChangeHandler()"
          v-model="ruleForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { validEmail, validTel } from "../util/validate.js";
import { addUser } from "../api/staff.js";
import moment from "moment";
import { mapState } from "vuex";
export default {
  name: "Admin-staff-add",
  computed: {
    ...mapState(["depart"]),
  },
  methods: {
    switchChangeHandler() {
      this.status = !this.status;
      console.log(this.status);
    },
    async addInfos() {
      await addUser(this.ruleForm);
    },
    submitForm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.ruleForm.date = moment(this.ruleForm.date).format("YYYY-MM-DD");
          // console.log("form", this.ruleForm);
          this.addInfos();
          this.$message({
            message: "信息添加成功！",
            type: "success",
          });
        } else {
          this.$message.error("信息填写不完整或格式不正确！");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs["ruleForm"].resetFields();
    },
  },
  data() {
    // 验证电话号码
    const validateTel = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入联系电话"));
      } else {
        if (!validTel(value)) {
          callback(new Error("电话号码格式不正确"));
        }
        callback();
      }
    };
    // 验证电子邮箱
    const validateEmail = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入电子邮箱"));
      } else {
        if (!validEmail(value)) {
          callback(new Error("电子邮箱格式不正确"));
        }
        callback();
      }
    };
    return {
      ruleForm: {
        name: "",
        sex: "",
        department: "",
        position: "",
        date: "",
        tel: "",
        email: "",
        address: "",
        status: true,
      },
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "姓名长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        department: [
          { required: true, message: "请选择部门", trigger: "blur" },
        ],
        position: [{ required: true, message: "请选择职位", trigger: "blur" }],
        date: [{ required: true, message: "日期不能为空", trigger: "blur" }],
        tel: [{ required: true, trigger: "blur", validator: validateTel }],
        email: [{ required: true, trigger: "blur", validator: validateEmail }],
        pass: [{ validator: validateTel, trigger: "blur" }],
        address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
      },
    };
  },
};
</script>

<style scoped lang="less">
.userAdd-form {
  width: 400px;
  height: 200px;
}
.el-radio--medium.is-bordered {
  margin: 10px;
}
</style>